package org.csu.pet_store_back.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import org.csu.pet_store_back.common.CommonResponse;
import org.csu.pet_store_back.domain.Account;
import org.csu.pet_store_back.domain.Order;
import org.csu.pet_store_back.persistence.OrderMapper;
import org.csu.pet_store_back.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    /**
     * 根据shopperId返回订单
     * */
    public CommonResponse<List<Order>> getAllOrders(String shopperId){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shopperid",shopperId);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        if (orderList==null){
            return CommonResponse.createForError("请求数据库失败或用户订单为空");
        } else {
            return CommonResponse.createForSuccess("请求成功",orderList);
        }
    }

    /**
     * 创建新订单,订单中包含多种商品时分别新增
     * */
    public CommonResponse<Order> newOrder(Order order){
        int result = orderMapper.insert(order);
        if (result == 1){
            return CommonResponse.createForSuccess(order);
        } else {
            return CommonResponse.createForError("服务器异常");
        }
    }

    /**
     * 更新订单之发货地址更新
     * */
    public CommonResponse<Order> updateOrderAddress(String orderId,String address){
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderid",orderId);
        updateWrapper.set("billaddress",address);
        int result = orderMapper.update(null,updateWrapper);
        if (result == 1){
            return CommonResponse.createForSuccessMessage("更新成功");
        } else {
            return CommonResponse.createForError("订单号不正确");
        }
    }

    /**
     * 更新订单之支付状态设置
     * */
    public CommonResponse<Order> updateOrderStatus(String orderId,String status, String date) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderid",orderId).set("paymentstatus",status).set("senddate",date);
        int result = orderMapper.update(null,updateWrapper);
        if (result == 1){
            return CommonResponse.createForSuccessMessage("更新成功");
        } else {
            return CommonResponse.createForError("订单号不正确");
        }
    }
}
